package com.umeng.push;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import com.hejia.alauda.entity.PushMessage;
import com.umeng.config.UmengConfig;
import com.umeng.push.android.AndroidBroadcast;
import com.umeng.push.android.AndroidCustomizedcast;
import com.umeng.push.ios.IOSBroadcast;
import com.umeng.push.ios.IOSCustomizedcast;

/**
 * 友盟推送
 * @author hyr
 *
 */
public class UmengPush {

	// 友盟消息推送服务提供的应用唯一标识和服务器秘钥
//	private static final String androidAppKey = "54365b17fd98c59b7703b66e";
//	private static final String androidAppMasterSecret = "ktmw4d7mq9fm0s8hcvz9evpkhjlaidrn";
//	private static final String iosAppkey = "5430d573fd98c521aa0380d9";
//	private static final String iosAppMasterSecret = "lmeke9z0kihkz1roqybockxlgoxpgcvt";
	
	private UmengConfig config;
	
	public UmengPush(UmengConfig config) {
		this.config = config;
	}
		
	/**
     * ios广播
     *
     * @param pushMessage 推送消息
     * @throws Exception
     */
    public boolean sendIOSBroadcast(PushMessage pushMessage) throws Exception {
    	
    	String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
		String validationToken = DigestUtils.md5Hex(config.getIOSAppkey().toLowerCase() + config.getIOSAppMasterSecret().toLowerCase() + timestamp);
		
    	IOSBroadcast iosBroadcast = new IOSBroadcast();
        iosBroadcast.setPredefinedKeyValue("appkey", config.getIOSAppkey());
        iosBroadcast.setPredefinedKeyValue("timestamp", timestamp);
        iosBroadcast.setPredefinedKeyValue("validation_token", validationToken);
        iosBroadcast.setPredefinedKeyValue("alert", pushMessage.getAlert());
        iosBroadcast.setPredefinedKeyValue("badge", 0);
        iosBroadcast.setPredefinedKeyValue("sound", "chime");

        // "production_mode":"true/false" 可选 正式/测试模式。测试模式下，广播/组播只会将消息发给测试设备。
        iosBroadcast.setPredefinedKeyValue("production_mode", pushMessage.getProductionMode());

        // 设置自定义参数
        if (StringUtils.isNotEmpty(pushMessage.getOpenUrl())) {
        	iosBroadcast.setCustomizedField("openUrl", pushMessage.getOpenUrl());
        }
        if (StringUtils.isNotEmpty(pushMessage.getOpenAppPage())) {
        	iosBroadcast.setCustomizedField("openAppPage", pushMessage.getOpenAppPage());
        }
        
        return iosBroadcast.send();
    }

    /**
     * 单播用别名的形式实现
     * @param pushMessage 推送消息
     * @throws Exception 
     */
	public boolean sendIOSCustomizedcast(PushMessage pushMessage) throws Exception {
		
		String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
		String validationToken = DigestUtils.md5Hex(config.getIOSAppkey().toLowerCase() + config.getIOSAppMasterSecret().toLowerCase() + timestamp);
		
		IOSCustomizedcast customizedcast = new IOSCustomizedcast();
        customizedcast.setPredefinedKeyValue("appkey", config.getIOSAppkey());
        customizedcast.setPredefinedKeyValue("timestamp", timestamp);
        customizedcast.setPredefinedKeyValue("validation_token", validationToken);
        // 设置设备别名
        customizedcast.setPredefinedKeyValue("alias", pushMessage.getUser().getExternal());
        // 设置别名类型
        customizedcast.setPredefinedKeyValue("alias_type", "EXTERNAL");

        // 消息内容展示
        customizedcast.setPredefinedKeyValue("alert", pushMessage.getAlert());
        customizedcast.setPredefinedKeyValue("badge", 0);
        customizedcast.setPredefinedKeyValue("sound", "chime");

        // "production_mode":"true/false" 可选 正式/测试模式。测试模式下，广播/组播只会将消息发给测试设备。
        customizedcast.setPredefinedKeyValue("production_mode", pushMessage.getProductionMode());
        
        // 设置自定义参数
        if (StringUtils.isNotEmpty(pushMessage.getOpenUrl())) {
        	customizedcast.setCustomizedField("openUrl", pushMessage.getOpenUrl());
        }
        if (StringUtils.isNotEmpty(pushMessage.getOpenAppPage())) {
        	customizedcast.setCustomizedField("openAppPage", pushMessage.getOpenAppPage());
        }
        
        return customizedcast.send();
	}

	/**
	 * Android广播
	 * @param pushMessage 推送消息
	 * @throws Exception 
	 */
	public boolean sendAndroidBroadcast(PushMessage pushMessage) throws Exception {
		
		String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
		String validationToken = DigestUtils.md5Hex(config.getAndroidAppKey().toLowerCase() + config.getAndroidAppMasterSecret().toLowerCase() + timestamp);
		
		AndroidBroadcast androidBroadcast = new AndroidBroadcast();
        androidBroadcast.setPredefinedKeyValue("appkey", config.getAndroidAppKey());
        androidBroadcast.setPredefinedKeyValue("timestamp", timestamp);
        androidBroadcast.setPredefinedKeyValue("validation_token", validationToken);
        
        androidBroadcast.setPredefinedKeyValue("ticker", "快聚财"); // 通知栏提示文字
        androidBroadcast.setPredefinedKeyValue("title", "快聚财");  // 通知标题
        androidBroadcast.setPredefinedKeyValue("text", pushMessage.getAlert());  // 通知文字描述
        androidBroadcast.setPredefinedKeyValue("after_open", "go_app");
        androidBroadcast.setPredefinedKeyValue("display_type", "notification"); // 消息类型，值可以为:notification-通知，message-消息
        
        // "production_mode":"true/false" 可选 正式/测试模式。测试模式下，广播/组播只会将消息发给测试设备。
        androidBroadcast.setPredefinedKeyValue("production_mode", pushMessage.getProductionMode());

        // 设置自定义参数
        if (StringUtils.isNotEmpty(pushMessage.getOpenUrl())) {
        	androidBroadcast.setExtraField("openUrl", pushMessage.getOpenUrl());
        }
        if (StringUtils.isNotEmpty(pushMessage.getOpenAppPage())) {
        	androidBroadcast.setExtraField("openAppPage", pushMessage.getOpenAppPage());
        }
        
        return androidBroadcast.send();
	}

	/**
	 * 安卓别名形式单播
	 * @param pushMessage 推送消息
	 * @throws Exception 
	 */
	public boolean sendAndroidCustomizedcast(PushMessage pushMessage) throws Exception {
		
		String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
		String validationToken = DigestUtils.md5Hex(config.getAndroidAppKey().toLowerCase() + config.getAndroidAppMasterSecret().toLowerCase() + timestamp);
		
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast();
        customizedcast.setPredefinedKeyValue("appkey", config.getAndroidAppKey());
        customizedcast.setPredefinedKeyValue("timestamp", timestamp);
        customizedcast.setPredefinedKeyValue("validation_token", validationToken);
        
        // 设置设备别名
        customizedcast.setPredefinedKeyValue("alias", pushMessage.getUser().getExternal());
        // 设置设备别名l类型
        customizedcast.setPredefinedKeyValue("alias_type", "EXTERNAL");
        
        customizedcast.setPredefinedKeyValue("ticker", "快聚财");
        customizedcast.setPredefinedKeyValue("title", "快聚财");
        customizedcast.setPredefinedKeyValue("text", pushMessage.getAlert());
        customizedcast.setPredefinedKeyValue("after_open", "go_app");
        customizedcast.setPredefinedKeyValue("display_type", "notification");

        // "production_mode":"true/false" 可选 正式/测试模式。测试模式下，广播/组播只会将消息发给测试设备。
        customizedcast.setPredefinedKeyValue("production_mode", pushMessage.getProductionMode());
        
        // 设置自定义参数
        if (StringUtils.isNotEmpty(pushMessage.getOpenUrl())) {
        	customizedcast.setExtraField("openUrl", pushMessage.getOpenUrl());
        }
        if (StringUtils.isNotEmpty(pushMessage.getOpenAppPage())) {
        	customizedcast.setExtraField("openAppPage", pushMessage.getOpenAppPage());
        }
        
        return customizedcast.send();
	}
}
